package com.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eduservice.entity.EduSubject;
import com.eduservice.entity.excel.SubjectData;
import com.eduservice.entity.subject.OneSubject;
import com.eduservice.entity.subject.TwoSubject;
import com.eduservice.listener.SubjectExcelListener;
import com.eduservice.mapper.EduSubjectMapper;
import com.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {



    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //课程分类列表功能（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> TwoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list用于存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来的所有一级分类list集合遍历 得到每一个一级分类对象 获取每个一级分类对象值
        //封装到要求的list集合里面List<OneSubject> finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {//遍历oneSubjectList集合
            //得到oneSubjectList每个eduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);

            //eduSubject获取出来放到oneSubject对象里面
            //多个oneSubject放到finalSubjectList里面
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            /**
             * BeanUtils.copyProperties(eduSubject,oneSubject);
             * 是spring中的方法
             * 作用是 将一个集合中的内容get出来 再set进第二个集合
             */
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //封装二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int l = 0; l < TwoSubjectList.size(); l++) {
                EduSubject eduSubject2 = TwoSubjectList.get(l);
                //判断二级分类的parentid和一级分类的id是否一样
                if (eduSubject2.getParentId().equals(eduSubject.getId())){
                    //把eduSubject2的值复制到twoSubject中
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            //把所有的二级分类放到一级分类中
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }
}
